<?php

/**
 * Maincategoryadmin_Model_Maincategoryadmin
 * main category admin model
 * @author Adrian Stolarski
 */
class Maincategoryadmin_Model_Maincategoryadmin implements 
Maincategoryadmin_Model_Mainable, 
        Maincategoryadmin_Model_Decidable {
    
    /**
     *
     * @var type $action_handler
     * action handler
     */
    public $action_handler;
    
    /**
     * main()
     * maincategory admin action decision
     * @param array $variables
     * @return type
     */
    public function main(array $variables = null) {
        //var_dump($variables);
        //die();
        return $this->model_decision($variables);
    }

    public function model_decision(array $variables = null) {
        switch ($variables['get']['name']){
            case "main_categories_list":
                //var_dump($variables);
                return $this->main_categories_list($variables);
                break;
            case "add_main_category":
                return $this->add_main_category($variables);
                break;
            case "insert_main_category":
                //var_dump($variables);
                //die();
                $this->insert_main_category($variables['post']);
                break;
            case "edit_main_category":
                return $this->edit_main_category($variables['post']);
                break;
            case "update_main_category":
                $this->update_main_category($variables['post']);
                break;
            case "delete_main_category":
                $this->delete_main_category($variables['post']);
            default:
                return $this->main_categories_list($variables);
                break;
        }
    }
    
    /**
     * delete_main_category()
     * delete main category
     * 
     * @param type $variables
     */
    private function delete_main_category($variables){
        //var_dump($variables);
        //die();
        if(isset($variables['csrf_token']) && ($variables['csrf_token'] == $_SESSION['csrf_token'])){
            $this->action_handler = new Maincategoryadmin_Data_Deletemaincategory;
            $this->action_handler->main($variables);
        }
        else {
            $this->redirect_action($variables);
        }
    }
    
    /**
     * update_main_category()
     * update main category
     * 
     * @param type $variables
     */
    private function update_main_category($variables){
        if(isset($variables['csrf_token']) && ($variables['csrf_token'] == $_SESSION['csrf_token'])){
            $this->action_handler = new Maincategoryadmin_Data_Updatemaincategory;
            $this->action_handler->main($variables);
        }
        else {
            $this->redirect_action($variables);
        }
        var_dump($variables);
    }
    
    /**
     * edit_main_category()
     * get edit main category template data
     * 
     * @param type $variables
     * @return type Maincategoryadmin_Data_Editmaincategory
     */
    private function edit_main_category($variables){
        //var_dump($variables);
        //die();
        if(isset($variables['csrf_token']) && ($variables['csrf_token'] == $_SESSION['csrf_token'])){
            $this->action_handler = new Maincategoryadmin_Data_Editmaincategory;
            return $this->action_handler->main($variables);
        }
        else {
            $this->redirect_action($variables);
        }
    }
    
    /**
     * insert_main_category()
     * insert main category 
     * 
     * @param type $variables
     */
    private function insert_main_category($variables){
        //var_dump($variables);
        //die();
        if(isset($variables['csrf_token']) && ($variables['csrf_token'] == $_SESSION['csrf_token'])){
            //var_dump($variables);
            //die();
            $this->action_handler = new Maincategoryadmin_Data_Insertmaincategory;
            $this->action_handler->main($variables);
        }
        else {
            $this->redirect_action($variables);
        }
    }
    
    /**
     * add_main_category()
     * return add main category template data
     * 
     * @param type $variables
     * @return type Maincategory_Data_Addmaincategory
     */
    private function add_main_category($variables){
        $this->action_handler = new Maincategoryadmin_Data_Addmaincategory;
        return $this->action_handler->main($variables);
    }
    
    /**
     * main_categories_list()
     * list of main category
     * 
     * @param type $variables
     * @return type MAincategory_Data_Maincategorieslist
     */
    private function main_categories_list($variables){
        //var_dump($variables);
        $this->action_handler = new Maincategoryadmin_Data_Maincategorieslist;
        return $this->action_handler->main($variables);
    }
    
    /**
     * redirect_action()
     * destroy session and login redirect
     * 
     * @param array $variables
     */
    private function redirect_action(array $variables = null){
        session_destroy();
        $this->action_handler = new Login_Data_Loggedredirect;
        $this->action_handler->main($variables);
    }

}
